home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 408_01 / addgroup.c < prev    next >
Text File  |  1993-08-06  |  8KB  |  244 lines

  1. /*
  2.     SNEWS 1.91
  3.  
  4.     addgroup - add a new newsgroup to the newsbase
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     Modifications copyright (C) 1993  Daniel Fandrich
  12.                         <dan@fch.wimsey.bc.ca> or CompuServe 72365,306
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License, version 1, as
  16.     published by the Free Software Foundation.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     See the file COPYING, which contains a copy of the GNU General
  24.     Public License.
  25.  
  26.  
  27.     Source is formatted with a tab size of 4.
  28.  
  29.  */
  30.  
  31. #include <io.h>
  32. #include <ctype.h>
  33. #include "defs.h"
  34. #include "getopt.h"
  35.  
  36. INFO my_stuff;
  37.  
  38.  
  39. /*---------------------------- main ---------------------------------------*/
  40. void main(int argc, char *argv[])
  41. {
  42.     /*
  43.      *  Add newsgroups.  All that has to be done is stick the new
  44.      *  group in the active file, after checking that is doesn't
  45.      *  already exist.  'It aint pretty, but it works.
  46.      */
  47.  
  48.     char   buf[256], buf2[256], *p, txt_name[20];
  49.     char   group_name[128];
  50.     int    added;
  51.     int       i;
  52.     int       post = 'y';        /* default posting status */
  53.     int    local = FALSE;    /* local group (not remote) */
  54.     time_t t, tlast = 0;
  55.     ACTIVE *gp;
  56.     FILE   *tmp;
  57.     FILE   *active_file, *new_active_file;
  58.  
  59.     signal(SIGINT, sig_break);        /* turn control-break off */
  60.  
  61.     fprintf(stderr, "ADDGROUP: (%s)\n\n", VERSION);
  62.  
  63.     if ((argc < 2) || ((argv[1][0] == '-') || (argv[1][0] == '/')) && ((argv[1][1] == '?') || (argv[1][1] == 'h'))) {
  64.         printf("usage: addgroup [-y] [-n] [-m] [-l] [-r] group.name [...]\n");
  65.         exit(2);
  66.     }
  67.  
  68.         if (!load_stuff()) {
  69.             fprintf(stderr, "Couldn't read rc info\n");
  70.         }
  71.  
  72.         /*
  73.          *  Check for active file.  If there is none:
  74.          *    - create it with a single entry 'junk'.
  75.          *    - ensure that the newsbase directory exists
  76.          *    - create an empty junk text file and index file
  77.          */
  78.         sprintf(buf, "%sactive", my_stuff.news_dir);
  79.         if (access(buf, 0) != 0) {
  80.             active_file = fopen(buf, "wb");
  81.             fprintf(active_file, "junk 00000000 00000000 00000000 y\n");
  82.             fclose(active_file);
  83.         }
  84.  
  85.         /* ensure that the news base file exists - just try to create it */
  86.         sprintf(buf, "%snewsbase", my_stuff.news_dir);
  87.         mkdir(buf);
  88.         sprintf(buf, "%snewsbase\\00000000", my_stuff.news_dir);
  89.         if (access(buf, 0) != 0) {
  90.             if ((tmp = fopen(buf, "wb")) == NULL) {
  91.                 fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  92.                 exit(1);
  93.             }
  94.             fclose(tmp);
  95.         }
  96.         sprintf(buf, "%snewsbase\\00000000.idx", my_stuff.news_dir);
  97.         if (access(buf, 0) != 0) {
  98.             if ((tmp = fopen(buf, "wb")) == NULL) {
  99.                 fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  100.                 exit(1);
  101.             }
  102.             fclose(tmp);
  103.         }
  104.  
  105.         load_active_file();
  106.         close_active();
  107.  
  108.  
  109.         for (i = 1; i < argc; i++) {
  110.  
  111.           if (argv[i][0] == '-') {
  112.                 switch (argv[i][1]) {
  113.                     case 'y':
  114.                     case 'n':
  115.                     case 'm':
  116.                         post=argv[i][1];
  117.                         break;
  118.  
  119.                     case 'l':
  120.                         local=TRUE;
  121.                         break;
  122.  
  123.                     case 'r':
  124.                         local=FALSE;
  125.                         break;
  126.  
  127.                     default:
  128.                         fprintf(stderr, "addgroup: illegal option %s -- aborting\n", argv[i]);
  129.                         close_active_file();
  130.                         exit(1);
  131.                 }
  132.           } else {
  133.  
  134.             /* check that the group doesn't already exist */
  135.             strlwr(strcpy(group_name, argv[i]));
  136.             gp = find_news_group(group_name);
  137.  
  138.             if ((stricmp(group_name, "junk") != 0) &&
  139.               (stricmp(gp->group, "junk") == 0)) {
  140.  
  141.                 printf("addgroup: creating group %s\n", group_name);
  142.  
  143.                 /* open the active file */
  144.                 sprintf(buf, "%sactive", my_stuff.news_dir);
  145.                 active_file = fopen(buf, "rb");
  146.  
  147.                 /* create new one */
  148.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  149.                 if ((new_active_file = fopen(buf, "wb")) == NULL) {
  150.                     fprintf(stderr, "addgroup: cannot create %s\n", buf);
  151.                     exit(1);
  152.                 }
  153.  
  154.                 added = FALSE;
  155.                 if (active_file != NULL) {
  156.  
  157.                     /* delay ensures names are different - ahem */
  158.                     while (tlast == time(&t))
  159.                         sleep(1);        /* be nice under OS/2 & sleep in loop */
  160.                     tlast = t;
  161.                     sprintf(txt_name, "%ld", t);
  162.                     if (strlen(txt_name) > 8) {
  163.                         strcpy(txt_name, txt_name+(strlen(txt_name)-8));
  164.                     }
  165.  
  166.                     /*
  167.                      *  Check newbase name is not already used.  If not
  168.                      *  create an empty newsbase and index files.
  169.                      */
  170.                     sprintf(buf, "%snewsbase\\%s", my_stuff.news_dir, txt_name);
  171.                     if (access(buf, 0) != 0) {
  172.  
  173.                         if ((tmp = fopen(buf, "wb")) == NULL) {
  174.                             fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  175.                             exit(1);
  176.                         }
  177.                         fclose(tmp);
  178.                         sprintf(buf, "%snewsbase\\%s.idx", my_stuff.news_dir, txt_name);
  179.                         if ((tmp = fopen(buf, "wb")) == NULL) {
  180.                             fprintf(stderr, "addgroup: cannot create file %s\n", buf);
  181.                             exit(1);
  182.                         }
  183.                         fclose(tmp);
  184.  
  185.                     } else {
  186.                         fprintf(stderr, "addgroup: newsbase name collision - try again\n");
  187.                         fclose(new_active_file);
  188.                         unlink(buf);
  189.                         exit(1);
  190.                     }
  191.  
  192.                     /* search for alphabetical position of group name in active file */
  193.                     while (fgets(buf, 255, active_file) != NULL) {
  194.  
  195.                         strcpy(buf2, buf);
  196.                         p = strtok(buf2, " \t");
  197.  
  198.                         if (local)
  199.                             group_name[0] = toupper(group_name[0]);
  200.                         if ((stricmp(group_name, p) < 0) && !added) {
  201.  
  202.                         /* found alphabetical position */
  203.                             fprintf(new_active_file, "%s %s 00000000 00000000 %c\n",
  204.                                 group_name, txt_name, post);
  205.                             added = TRUE;
  206.                         }
  207.  
  208.                         if (fputs(buf, new_active_file) == EOF) {
  209.                             fprintf(stderr, "addgroup: couldn't write new active file\n");
  210.                         }
  211.  
  212.                     }
  213.                 }
  214.  
  215.                 /* group name makes it alphabetically last in file */
  216.                 if (!added) {
  217.                     if (local)
  218.                         group_name[0] = toupper(group_name[0]);
  219.                     fprintf(new_active_file, "%s %s 00000000 00000000 %c\n",
  220.                                 group_name, txt_name, post);
  221.                 }
  222.  
  223.                 fclose(active_file);
  224.                 fclose(new_active_file);
  225.  
  226.                 sprintf(buf, "%sactive.bak", my_stuff.news_dir);
  227.                 unlink(buf);
  228.                 sprintf(buf2, "%sactive", my_stuff.news_dir);
  229.                 rename(buf2, buf);
  230.                 sprintf(buf, "%sactive.new", my_stuff.news_dir);
  231.                 rename(buf, buf2);
  232.  
  233.  
  234.             } else {
  235.                 fprintf(stderr, "addgroup: newsgroup %s already exists\n", group_name);
  236.             }
  237.           }
  238.  
  239.         } /* for */
  240.  
  241.         close_active_file();
  242.  
  243. }
  244.